<?php

class Default_ContactoController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->headLink(array('type' => 'image/x-icon', 'rel' => 'shortcut icon', 'href' => '/images/favicon.ico'), 'APPEND');
        $this->view->headLink()->appendStylesheet('/css/paginas.css');
        $this->view->headLink()->appendStylesheet('/css/forms.css');
        $this->view->headScript()->offsetSetFile(1, '/js/jquery-1.3.2.min.js');
        $this->view->headScript()->offsetSetFile(2, '/js/jquery.form.js');
        $this->view->headScript()->offsetSetFile(3, '/js/sitio.js');

        $this->_settings = Zend_Registry::get('settings');
        
    }

    public function indexAction()
    {
        $form = new default_Form_Contacto();
        $form->setAction('/contacto/enviar/');
        $this->view->titulo = 'Contacto';
        $this->view->form = $form;
    }

    public function enviarAction()
    {

        $this->_helper->layout->disableLayout();

        $form =  new default_Form_Contacto();
        $form->setAction('/contacto/enviar/');

        if ($this->getRequest()->isPost()) {

            $params = $this->_request->getPost();

            if (!$form->isValid($params)) {

                $form->populate($params);
                $this->view->form = $form;
                $this->view->exito = false;

            } else {

                $contacto = new admin_Model_Contacto();
                $fecha = new Zend_Date();

                $contacto->setAsunto($form->asunto->getValue());
                $contacto->setNombre($form->nombre->getValue());
                $contacto->setEmail($form->email->getValue());
                $contacto->setEmpresa($form->empresa->getValue());
                $contacto->setPais($form->pais->getValue());
                $contacto->setTelefono($form->telefono->getValue());
                $contacto->setMensaje($form->mensaje->getValue());
                $contacto->setFecha($fecha->get(Zend_Date::ISO_8601));
                $contacto->setIp($this->_request->getServer('REMOTE_ADDR'));

                $contactoModel = new admin_Model_ContactoMapper();
                $contactoModel->agregar($contacto);

                $contenidoMail = 'Nombre : '.$contacto->getNombre() ."\r\n";
                $contenidoMail .= 'Fecha  : '.$contacto->getFecha() ."\r\n";
                $contenidoMail .= 'Mensaje : '.$contacto->getMensaje() ."\r\n";

                try {

                    $mail = new Zend_Mail();

                    $mail->setBodyText($contenidoMail);
                    $mail->addTo($this->_settings->aplicacion->admin->contacto, $this->_settings->aplicacion->admin->responsable);
                    $mail->setSubject($contacto->getAsunto());
                    $mail->send();


                } catch (Zend_Mail_Exception $e) {

                    $log = $this->_getLog();
                    $errors = $this->_getParam('error_handler');

                    if ($log) {
                        $log->crit($e->getMessage(), $errors->exception);
                    }
                }

                $this->view->exito = true;
            }
        }
    }

}



